package com.ruoyi.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.school.domain.PacClassTeacher;
import com.ruoyi.school.domain.PacTeacher;
import com.ruoyi.school.domain.vo.PacClassInfoVo;
import com.ruoyi.school.service.PacClassService;
import com.ruoyi.school.service.PacClassTeacherService;
import com.ruoyi.school.mapper.PacClassTeacherMapper;
import com.ruoyi.school.service.PacTeacherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author jeremylin
* @description 针对表【pac_class_teacher(班级与老师的绑定关系表)】的数据库操作Service实现
* @createDate 2025-07-09 21:35:18
*/
@Service
public class PacClassTeacherServiceImpl extends ServiceImpl<PacClassTeacherMapper, PacClassTeacher>
    implements PacClassTeacherService{

    @Resource
    private PacTeacherService pacTeacherService;

    @Resource
    private PacClassService pacClassService;

    /**
     * @param id
     * @return
     */
    @Override
    public List<PacClassTeacher> getListByClassId(Integer id) {
        return list(new QueryWrapper<PacClassTeacher>().eq("class_id", id));
    }

    /**
     * 查询老师绑定的班级
     *
     * @param user
     * @return
     */
    @Override
    public List<PacClassInfoVo> selectByTeacher(SysUser user) {
        PacTeacher byUserId = pacTeacherService.getByUserId(user.getUserId());
        if(byUserId == null) return null;
        List<PacClassTeacher> list = lambdaQuery().eq(PacClassTeacher::getTeacherId, byUserId.getId()).list();
        List<Long> collect = list.stream().map(item -> item.getClassId().longValue()).collect(Collectors.toList());
        List<PacClassInfoVo> pacClassInfoVos = pacClassService.selectClassTeacherByIds(collect);
        return pacClassInfoVos;
    }

    /**
     * 查询老师绑定的班级信息，权限查询
     *
     * @param user
     * @param teacher
     * @return
     */
    @Override
    public List<PacClassInfoVo> selectByTeacherByKey(SysUser user, String teacher) {
        PacTeacher byUserId = pacTeacherService.getByUserId(user.getUserId());
        if(byUserId == null) return null;
        List<PacClassTeacher> list = lambdaQuery()
                .eq(PacClassTeacher::getTeacherId, byUserId.getId())
                .eq(PacClassTeacher::getRoleKey, teacher)
                .list();
        List<Long> collect = list.stream().map(item -> item.getClassId().longValue()).collect(Collectors.toList());
        List<PacClassInfoVo> pacClassInfoVos = pacClassService.selectClassTeacherByIds(collect);
        return pacClassInfoVos;
    }
}




